class Solution:
    def rob(self, nums: list) -> int:
        if len(nums) <= 3:
            return max(nums)
        return max(self.rob2(nums[1:]), self.rob2(nums[:-1]))

    def rob2(self, nums: list) -> int:
        nums[2] = max(nums[0] + nums[2], nums[1])
        for i in range(3, len(nums)):
            nums[i] = max(nums[i - 2] + nums[i], nums[i - 3] + nums[i], nums[i - 1])
        return nums[-1]


a = Solution()
print(a.rob([1, 2, 3, 1]))
